<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use App\Tag;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
//        auth()->loginUsingId(1);


        //
//        Schema::defaultStringLength(1000);

        Validator::extend('status', function ($attribute, $value, $parameters) {
            return preg_match('/[0-3]/', $value);
        });

        Validator::extend('tag', function ($attribute, $value, $parameters) {
            for ($i = 0; $i < sizeof($value); $i++) {
                $temp = true;
                if (preg_match('/^[1-9][0-9]*$/', $value[$i]) != 0) {
                    $temp = true;
                } else {
                    $temp = false;
                    break;
                }
            }
            return $temp;
        });

        Validator::extend('tag_id', function ($attribute, $value, $parameters) {
            for ($i = 0; $i < sizeof($value); $i++) {
                $temp = true;
                $result = Tag::where("id", $value[$i])->first();
                if (!empty($result)) {
                    $temp = true;
                } else {
                    $temp = false;
                    break;
                }
            }
            return $temp;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
